package flc.ast.adapter;

import android.text.format.Formatter;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import flc.ast.R;
import flc.ast.bean.MyFileBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.utils.MimeUtils;

public class FileAdapter extends StkProviderMultiAdapter<MyFileBean> {

    public FileAdapter() {
        addItemProvider(new FileItemProvider());
    }

    private class FileItemProvider extends BaseItemProvider<MyFileBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_file;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyFileBean bean) {
            boolean isShow = false;
            ImageView ivFileIcon = baseViewHolder.getView(R.id.ivFileIcon);
            if (baseViewHolder.getBindingAdapterPosition() == 0) {
                baseViewHolder.getView(R.id.line).setVisibility(View.GONE);
            }
            if (FileUtils.isDir(bean.getPath())) {
                ivFileIcon.setImageResource(R.drawable.wjj);
            } else if (MimeUtils.isImgMimeType(bean.getPath())) {
                Glide.with(baseViewHolder.itemView).load(bean.getPath()).into(ivFileIcon);
            } else if (MimeUtils.isVideoMimeType(bean.getPath())) {
                Glide.with(baseViewHolder.itemView).load(bean.getPath()).into(ivFileIcon);
                isShow = true;
            } else if (MimeUtils.isAudioMimeType(bean.getPath())) {
                ivFileIcon.setImageResource(R.drawable.yinpin);
            } else if (bean.getPath().endsWith(".doc") || bean.getPath().endsWith(".docx")) {
                ivFileIcon.setImageResource(R.drawable.dox);
            } else if (bean.getPath().endsWith(".xls") || bean.getPath().endsWith(".xlsx")) {
                ivFileIcon.setImageResource(R.drawable.xls);
            } else if (bean.getPath().endsWith(".ppt") || bean.getPath().endsWith(".pptx")) {
                ivFileIcon.setImageResource(R.drawable.ppt);
            } else if (bean.getPath().endsWith(".zip")) {
                ivFileIcon.setImageResource(R.drawable.ysb);
            } else if (bean.getPath().endsWith(".pdf")) {
                ivFileIcon.setImageResource(R.drawable.pdf);
            } else if (bean.getPath().endsWith(".txt")) {
                ivFileIcon.setImageResource(R.drawable.txt);
            } else {
                ivFileIcon.setImageResource(R.drawable.qtwj);
            }
            baseViewHolder.setVisible(R.id.llFileItemVideo, isShow);
            baseViewHolder.setText(R.id.tvFileName, bean.getName());
            String phoneTime = TimeUtils.millis2String(bean.getDateModified() * 1000L, "yyyy-MM-dd HH:mm");
            baseViewHolder.setText(R.id.tvFileDate, phoneTime);
        }
    }
}
